function reverseList(head) {
  let pre = null
  let curr = head
  while(curr) {
    let temp = curr.next;
    curr.next = pre;
    pre = curr;
    curr = temp
  }
  return pre
}

function ListNode(val,next) {
  this.val = val || 0;
  this.next = next || null
}
function arryToList(arr) {
  return arr.reduceRight((acc, curr)=>new ListNode(curr, acc), null)
}
const head = [1,2,3,4,5]
const listHead = arryToList(head)
const res = reverseList(listHead)
console.log(JSON.stringify(res))
